/**
 * Copyright (c) 2011-2014, homeant (homeanter@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xin.homeant.common.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.session.InvalidSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;


/**
 * <P>
 * 控制器基类
 * </P>
 * 
 * @author homeant homeanter@163.com
 * @Data 2017年5月23日 下午1:27:45
 */
public abstract class BaseController {
	private final static Logger _log = LoggerFactory.getLogger(BaseController.class);
	
	
	/**
	 * 统一异常处理
	 * @param request
	 * @param response
	 * @param exception
	 */
	@ExceptionHandler
	public String exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
		_log.error("统一异常处理：", exception);
		request.setAttribute("ex", exception);
		// shiro没有权限异常
		if (exception instanceof UnauthorizedException) {
			response.setStatus(403);
			return "/403";
		}
		// shiro会话已过期异常
		if (exception instanceof InvalidSessionException) {
			response.setStatus(401);
			return "/400";
		}
		response.setStatus(400);
		return "/400";
	}
}
